//  Query the az available for the Region
data "huaweicloud_availability_zones" "az" {}

data "huaweicloud_compute_flavors" "flavors" {
  availability_zone = local.availability_zone
  performance_type  = "normal"
  cpu_core_count    = 2
  memory_size       = 4
}

//  Query available RDS for flavor
data "huaweicloud_rds_flavors" "flavors" {
  db_type           = "MySQL"
  db_version        = "8.0"
  instance_mode     = "single"
  vcpus             = 2
  memory            = 4
  availability_zone = local.availability_zone
}

locals {
  //  you need to specify a unique name under a tenant according to the business, which will be used as part of the resource name
  app_id                = "rf_app"
  //  Billing model for cloud resources
  charging_mode         = "postPaid"
  //  The billing model for bandwidth
  bandwidth_charge_mode = "traffic"
  db_port               = 3306
  availability_zone     = data.huaweicloud_availability_zones.az.names[0]
  //  The mapping relationship between Region and intranet DNS is different for different regions, for details, refer to:https://support.huaweicloud.com/dns_faq/dns_faq_002.html
  region_dns_map = {
    cn-north-4 = ["100.125.1.250", "100.125.129.250"]
    cn-south-1 = ["100.125.1.250", "100.125.136.29"]
  }
  
  //  Mapping of Region and imageId, required, please add all regions supported by your offering.
  image_ids_map = {
    cn-north-4 = ""
    cn-south-1 = ""
  }
}

// Create a VPC
module "vpc" {
  source = "../../modules/vpc"

  vpc_name            = "vpc_${local.app_id}"
  subnet_name         = "subnet_${local.app_id}"
  secgroup_name       = "secgroup_${local.app_id}"
  rds_secgroup_name   = "rds_secgroup_${local.app_id}"
  availability_zone   = local.availability_zone
  vpc_cidr            = "192.168.0.0/16"
  remote_ip_cidr      = var.remote_ip_cidr
  vpc_subnet_cidr     = var.vpc_subnet_cidr
  vpc_subnet_gateway_ip = "192.168.10.1"
  vpc_subnet_dns_list = local.region_dns_map[var.region]
  allow_ports_configuration = [
    {description = null, direction = "ingress", ethertype = "IPv4", protocol = "tcp", ports = "20-9527",remote_ip_prefix = var.remote_ip_cidr},
    {description = null, direction = "egress", ethertype = "IPv4", protocol = "tcp", ports = "3306",remote_ip_prefix = var.vpc_subnet_cidr},
  ]
  rds_ingress_allow_ports_configuration = [
    {description = null, direction = "ingress", ethertype = "IPv4", protocol = "tcp", ports = "3306",remote_ip_prefix = var.vpc_subnet_cidr}
  ]
}

// To create an RDS, if you do not need to create an RDS, delete the code and the modules/rds directory
module "rds" {
  source = "../../modules/rds"

  charging_mode            = local.charging_mode
  db_instance_name         = "rds_${local.app_id}"
  db_availability_zone     = [local.availability_zone]
  time_zone                = "UTC"
  vpc_id                   = module.vpc.vpc_id
  subnet_id                = module.vpc.subnet_id
  security_group_id        = module.vpc.rds_secgroup_id[0]
  flavor                   = data.huaweicloud_rds_flavors.flavors.flavors[0].name
  db_engines               = "MySQL"
  db_version               = "8.0"
  db_password              = var.db_password
  db_port                  = local.db_port
  group_type               = "normalLocalssd"
  disk_encryption_id       = ""
  param_group_id           = ""
  keep_days                = 100
  start_time               = "01:00-02:00"
  db_instance_storage_type = "CLOUDSSD"
  db_allocated_storage     = var.db_allocated_storage
  
}

// Create an ECS
module "ecs" {
  source = "../../modules/ecs"

  region               = var.region
  ecs_configuration    = [
    {name = "ecs_${local.app_id}_1",system_disk_type = "SAS", system_disk_size = 40, stop_before_destroy = true, delete_disks_on_termination = true, tags = {}}
  ]
  associated_configuration = [
    {public_ip = module.eip.address[0]}
  ]
  image_id             = local.image_ids_map[var.region]
  flavor_id            = data.huaweicloud_compute_flavors.flavors.ids[0]
  images_visibility    = "public"
  availability_zone    = local.availability_zone
  charging_mode        = local.charging_mode
  secgroup_id          = module.vpc.secgroup_id
  subnet_id            = module.vpc.subnet_id
  user_data            = "#!/bin/bash\necho 'root:${var.ecs_password}' | chpasswd"
}

//  Create an EIP, if you do not need to create an EIP, delete the code and modules/eip directory
module "eip" {
  source = "../../modules/eip"
  
  charging_mode         = local.charging_mode
  eip_configuration = [
    {type = "5_bgp", bandwidth_name = "bandwidth_${local.app_id}_1", size = 1, share_type = "PER", bandwidth_charge_mode = "traffic", name = "eip${local.app_id}_1", tags = {}}
  ]
}

//  To create a data disk for ECS, if you do not need to bind the data disk, delete the code and modules/evs directory
module "evs" {
  source = "../../modules/evs"
  
  volume_configuration = [
    {name = "evs_${local.app_id}_1", volume_type="SAS", volume_size=40,tags = {}}
  ]
  attached_configuration = [
    {instance_id = module.ecs.ecs_ids[0]}
  ]
  availability_zone = local.availability_zone
  charging_mode     = local.charging_mode
}
